<?php

namespace App\Console\Commands;

use App\Models\Article;
use Illuminate\Console\Command;

class BaiduPushConsole extends Command
{
    /**
     * The name and signature of the console command.
     * @var string
     */
    protected $signature = 'BaiduPushConsole';

    /**
     * The console command description.
     * @var string
     */
    protected $description = '百度站长url推送';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->info('========' . date('Y-m-d H:i:s') . '========');

        $this->pushNew();
        $this->pushEdit();
    }

    /**
     * 发起 POST 网络请求
     * @param string $url 目标地址
     * @param array|string $data 发送的数据
     * @param array $header 自定义header头
     * @return bool|string
     */
    private function httpPost($url, $data, $header = [])
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_TIMEOUT, 60);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }

    /**
     * 推送昨天发布的文章
     */
    private function pushNew()
    {
        $time1 = strtotime('-1 day 00:00:00');
        $time2 = strtotime('-1 day 23:59:59');

        $newList = Article::where('status', 1)
            ->whereBetween('published_at', [$time1, $time2])
            ->select('id', 'title', 'published_at')
            ->get();

        $newArr = [];
        $appUrl = config('app.url');
        $token = config('blog.baidu_zz_token');

        foreach ($newList as $article) {
            $newArr[] = $appUrl . $article->url;
        }

        if ($newArr) {
            $api = 'http://data.zz.baidu.com/urls?site=' . $appUrl . '&token=' . $token;
            $urls = implode("\n", $newArr);
            $header = ['Content-Type: text/plain'];
            $result = $this->httpPost($api, $urls, $header);

            $this->info("新增推送结果：{$result}");
            $this->info("新增的url：\n{$urls}");
        }
    }

    /**
     * 推送昨天更新的文章
     */
    private function pushEdit()
    {
        $time1 = strtotime('-1 day 00:00:00');
        $time2 = strtotime('-1 day 23:59:59');

        $upList = Article::where('status', 1)
            ->where('published_at', '<', $time1)
            ->whereBetween('updated_at', [$time1, $time2])
            ->select('id', 'title', 'published_at')
            ->get();

        $editArr = [];
        $appUrl = config('app.url');
        $token = config('blog.baidu_zz_token');

        foreach ($upList as $article) {
            $editArr[] = $appUrl . $article->url;
        }

        if ($editArr) {
            $api = 'http://data.zz.baidu.com/update?site=' . $appUrl . '&token=' . $token;
            $urls = implode("\n", $editArr);
            $header = ['Content-Type: text/plain'];
            $result = $this->httpPost($api, $urls, $header);

            $this->info("更新推送结果：{$result}");
            $this->info("更新的url：\n{$urls}");
        }
    }
}
